package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Point;
import model.Reader;

public class Graphwindow extends JPanel {
	Graph graph;
	
	public Graphwindow(String filename) {
		this.setLayout(new BorderLayout());

		graph = new Graph();
		setEpisode(new File("1.gpx"));
		add(graph, BorderLayout.CENTER);
		add(new JLabel("hello"), BorderLayout.NORTH);
		
		JPanel eastpane = new JPanel();
		eastpane.setLayout(new BoxLayout(eastpane, BoxLayout.PAGE_AXIS));
		add(eastpane, BorderLayout.EAST);
		eastpane.setMinimumSize(new Dimension(300, 0));
		
		JTextField yfield = new Yfield();
		eastpane.add(yfield);

		JTextField smoothness = new Smoothfield();
		eastpane.add(smoothness);

	}

	public void setEpisode(File file) {
		List<Point> points = Reader.read(file);
		if (points != null) {
			graph.show(points);
		}
		graph.repaint();
		graph.validate();
	}
	
	class Yfield extends JTextField implements ActionListener {
		
		public Yfield() {
			super("y-axis");
			addActionListener(this);
			
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			double y;
			try {
				y = Double.parseDouble(getText());
			} catch (Exception e) {
				return;
			}
			
			graph.setYmax(y);
			graph.repaint();
		}
	}
	
	class Smoothfield extends JTextField implements ActionListener {
		
		public Smoothfield() {
			super("smoothness");
			addActionListener(this);
			
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			int s;
			try {
				s = Integer.parseInt(getText());
			} catch (Exception e) {
				return;
			}
			
			graph.smoothness = s;
			graph.repaint();
		}
	}
}
